import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_getx_project/main/main_state.dart';
import 'package:flutter_getx_project/res/string_res/str_res_keys.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:x_project/x_project.dart';

/// Created by XieXin on 2022/8/23.
/// 业务逻辑处理
class MainController extends GetxController {
  final GlobalController globalController = Get.find<GlobalController>();
  MainState state = MainState();
  late PageController pageController;

  MainController() {
    ever<EventEntity>(globalController.event, (value) {
      LogUtil.v("global code========>${value.code}");
      switch (value.code) {
        case EventEntity.mainTabIndex0:
          setTabIndex(0);
          break;
        case EventEntity.mainTabIndex1:
          setTabIndex(1);
          break;
        case EventEntity.mainTabIndex2:
          setTabIndex(2);
          break;
        case EventEntity.mainTabIndex3:
          setTabIndex(3);
          break;
      }
    });
  }

  @override
  void onInit() {
    super.onInit();
    pageController = PageController(initialPage: state.index);
    initTab();
  }

  void setTabIndex(int index) {
    state.index = index;
    pageController.jumpToPage(state.index);
    update();
  }

  void initTab() {
    state.tabTitles = [SRMain.home.tr, SRMain.mine.tr];
    state.tabIcons = [
      [
        Icon(
          const IconData(0xe801, fontFamily: "Icons"),
          size: 50.w,
          color: XColors.textColor99,
        ),
        Icon(
          const IconData(0xe904, fontFamily: "Icons"),
          size: 50.w,
          color: Get.theme.primaryColor,
        ),
      ],
      [
        Icon(
          const IconData(0xe736, fontFamily: "Icons"),
          size: 50.w,
          color: XColors.textColor99,
        ),
        Icon(
          const IconData(0xe735, fontFamily: "Icons"),
          size: 50.w,
          color: Get.theme.primaryColor,
        ),
      ],
    ];
  }
}
